'''
Descripttion: 
version: 
Author: Zhang Lingxin
Date: 2023-05-30 21:03:27
LastEditors: Zhang Lingxin
LastEditTime: 2023-06-05 18:57:25
'''
from django.shortcuts import render, HttpResponse

# Create your views here.
from rest_framework import mixins, viewsets

from question.models import Program
from record.models import *
from record.serializers import *

from django.views.decorators.csrf import csrf_exempt
from . import recognize

class ChoiceRecordListViewSet(mixins.ListModelMixin, mixins.CreateModelMixin, viewsets.GenericViewSet):
    """选择题练习记录"""
    # 数据集
    queryset = ChoiceRecord.objects.all()
    # 序列化
    serializer_class = ChoiceRecordSerializer

    def get_queryset(self):
        # 模拟练习ID
        practice_id = self.request.query_params.get('practice_id')
        if practice_id:
            self.queryset = ChoiceRecord.objects.filter(practice_id=practice_id)
        return self.queryset


class FillRecordListViewSet(mixins.ListModelMixin, mixins.CreateModelMixin, viewsets.GenericViewSet):
    """填空题练习记录"""
    # 数据集
    queryset = FillRecord.objects.all()
    # 序列化
    serializer_class = FillRecordSerializer

    def get_queryset(self):
        # 模拟练习ID
        practice_id = self.request.query_params.get('practice_id')
        if practice_id:
            self.queryset = FillRecord.objects.filter(practice_id=practice_id)
        return self.queryset


class JudgeRecordListViewSet(mixins.ListModelMixin, mixins.CreateModelMixin, viewsets.GenericViewSet):
    """选择题练习记录"""
    # 数据集
    queryset = JudgeRecord.objects.all()
    # 序列化
    serializer_class = JudgeRecordSerializer

    def get_queryset(self):
        # 模拟练习ID
        practice_id = self.request.query_params.get('practice_id')
        if practice_id:
            self.queryset = JudgeRecord.objects.filter(practice_id=practice_id)
        return self.queryset


class ProgramRecordListViewSet(mixins.ListModelMixin, mixins.CreateModelMixin, viewsets.GenericViewSet):
    """编程题练习记录"""
    # 数据集
    queryset = ProgramRecord.objects.all()
    # 序列化
    serializer_class = ProgramRecordSerializer

    def get_queryset(self):
        # 模拟练习ID
        practice_id = self.request.query_params.get('practice_id')
        if practice_id:
            self.queryset = ProgramRecord.objects.filter(practice_id=practice_id)
        return self.queryset

class TextRecordListViewSet(mixins.ListModelMixin, mixins.CreateModelMixin, viewsets.GenericViewSet):
    """填空题练习记录"""
    # 数据集
    queryset = TextRecord.objects.all()
    # 序列化
    serializer_class = TextRecordSerializer

    def get_queryset(self):
        # 模拟练习ID
        practice_id = self.request.query_params.get('practice_id')
        if practice_id:
            self.queryset = TextRecord.objects.filter(practice_id=practice_id)
        return self.queryset
    

# ===================================================================== exam ==================================================================


class ChoiceRecordexamListViewSet(mixins.ListModelMixin, mixins.CreateModelMixin, viewsets.GenericViewSet, mixins.DestroyModelMixin):
    queryset = ChoiceRecordexam.objects.all()
    serializer_class = ChoiceRecordExamSerializer

    def get_queryset(self):
        paper_id = self.request.query_params.get('paper_id')
        if paper_id:
            self.queryset = ChoiceRecordexam.objects.filter(paper_id=paper_id)
        return self.queryset


class FillRecordexamListViewSet(mixins.ListModelMixin, mixins.CreateModelMixin, viewsets.GenericViewSet, mixins.DestroyModelMixin):
    queryset = FillRecordexam.objects.all()
    serializer_class = FillRecordExamSerializer

    def get_queryset(self):
        paper_id = self.request.query_params.get('paper_id')
        if paper_id:
            self.queryset = FillRecordexam.objects.filter(paper_id=paper_id)
        return self.queryset


class JudgeRecordexamListViewSet(mixins.ListModelMixin, mixins.CreateModelMixin, viewsets.GenericViewSet, mixins.DestroyModelMixin):

    queryset = JudgeRecordexam.objects.all()
    serializer_class = JudgeRecordExamSerializer

    def get_queryset(self):
        paper_id = self.request.query_params.get('paper_id')
        if paper_id:
            self.queryset = JudgeRecordexam.objects.filter(paper_id=paper_id)
        return self.queryset


class ProgramRecordexamListViewSet(mixins.ListModelMixin, mixins.CreateModelMixin, viewsets.GenericViewSet, mixins.DestroyModelMixin):

    queryset = ProgramRecordexam.objects.all()
    serializer_class = ProgramRecordExamSerializer

    def get_queryset(self):
        paper_id = self.request.query_params.get('paper_id')
        if paper_id:
            self.queryset = ProgramRecordexam.objects.filter(paper_id=paper_id)
        return self.queryset

class TextRecordexamListViewSet(mixins.ListModelMixin, mixins.CreateModelMixin, viewsets.GenericViewSet, mixins.DestroyModelMixin):
    queryset = TextRecordexam.objects.all()
    serializer_class = TextRecordExamSerializer

    def get_queryset(self):
        paper_id = self.request.query_params.get('paper_id')
        if paper_id:
            self.queryset = TextRecordexam.objects.filter(paper_id=paper_id)
        return self.queryset
    

# =======================================================================================================

@csrf_exempt
def handle_upload_page(request):
    return render(request, 'uploadPaper.html')

@csrf_exempt
def handle_uploaded_file(f, filename):
    with open(filename, 'wb+') as destination:
        for chunk in f.chunks():
            destination.write(chunk)
    print('handle_uploaded_file ok')

@csrf_exempt
def tea_upload_paper(request):
    if request.method == 'POST':
        file = request.FILES['myfile']
        filename = request.POST.get('filename')
        filename = 'fileUpload.txt'
        handle_uploaded_file(file, 'fileUpload.txt')
        recognize.recogniQues(filename)
        return HttpResponse('File uploaded successfully.')
    else:
        return HttpResponse('use post')